namespace Profit.ViewModels
{
    using System.Collections.ObjectModel;
    using System.Windows.Controls;
    using System.Windows.Input;
    using Core.Contracts.Interfaces;
    using Views.Interfaces;

    public sealed class FittingsViewModel : ViewModelBase<IFittingsView>
    {
        private readonly ObservableCollection<IFitting> _fittings;
        private IFitting _selectedFitting;
        private readonly ICommand _deleteFittingCommand;
        private readonly ICommand _exportFittingCommand;

        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="view">The IView instance associated</param>
        public FittingsViewModel(IFittingsView view) 
            : base(view)
        {
            view.ListViewMouseDoubleClick += ListViewMouseDoubleClickHandler;
        }

        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="view">The IView instance associated</param>
        /// <param name="fittings"></param>
        /// <param name="deleteFittingCommand"></param>
        /// <param name="exportFittingCommand"></param>
        public FittingsViewModel(IFittingsView view, ObservableCollection<IFitting> fittings, ICommand deleteFittingCommand, ICommand exportFittingCommand)
            : this(view)
        {
            _fittings = fittings;
            _deleteFittingCommand = deleteFittingCommand;
            _exportFittingCommand = exportFittingCommand;
        }

        public ICommand DeleteFittingCommand
        {
            get { return _deleteFittingCommand; }
        }

        public ICommand ExportFittingCommand
        {
            get { return _exportFittingCommand; }
        }

        /// <summary>
        /// Obtains the fittings collection
        /// </summary>
        public ObservableCollection<IFitting> Fittings
        {
            get { return _fittings; }
        }
        /// <summary>
        /// Obtains or defines the selected fitting
        /// </summary>
        public IFitting SelectedFitting
        {
            get { return _selectedFitting; }
            set { _selectedFitting = value;  }
        }

        private void ListViewMouseDoubleClickHandler(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            var list = sender as ListView;
            if (list == null || list.SelectedItem == null) return;

            SelectedFitting = list.SelectedItem as IFitting;
            NotifyPropertyChange(() => SelectedFitting);
        }
    }
}